﻿using System;
using System.Security.Principal;
using System.Threading;

namespace ConsoleAuthentication
{
    class Program
    {
        static void Main(string[] args)
        {
            var identity = new GenericIdentity("Miss Chen");
            var principal = new GenericPrincipal(identity, new string[] { "Admin", "User" });

            // 并非必需，但在 WinFrom 程序中会很有用
            Thread.CurrentPrincipal = principal;

            string loginName = principal.Identity.Name;
            bool isLogin = principal.Identity.IsAuthenticated;
            bool isAdmin = principal.IsInRole("Admin");
            bool isUser = principal.IsInRole("User");

            Console.WriteLine($"Current user:{loginName}");
            Console.WriteLine($"Is login?{isLogin}");
            Console.WriteLine($"Is admin?{isAdmin}");
            Console.WriteLine($"Is user?{isUser}");

            Console.Read();
        }
    }
}
